$(function() {
	
	$('#li_sys').addClass('active open');
	$('#dbconfig').addClass('active');
	
	//$("#username").val(''); 
	//$("#password").val('');
	
	$("#connecttest").click(function() {
		var ip=$("#serverip").val();
		var port=$("#port").val();
		var dbtype=$("#dbtype").val();
		var username=$("#username").val();
		var password=$("#password").val();
		var sid=$("#sid").val();
		
		$.ajax({
			type : 'post',
			url : 'dbconfig/test',
			dataType : 'text',					
			data : {
				"ip":ip,
				"port":port,
				"dbtype":dbtype,
				"username" : username,
				"password" : password,
				"sid":sid
			},
			success : function(msg) {
				console.log(msg);
				if(msg=="0"){
					alert("连接成功");
				}else{
					alert("连接失败");
				}
			},
			error : function(msg) {
				console.log('failed')
			}
		});    	
		
	});

	$("#saveconfig").click(function() {
		var sname=$("#servername").val();
		var ip=$("#serverip").val();
		var port=$("#port").val();
		var dbtype=$("#dbtype").val();
		var username=$("#username").val();
		var password=$("#password").val();
		var defaultdb=$("#defaultdb").val();
		var sid=$("#sid").val();
		
		$.ajax({
			type : 'post',
			url : 'dbconfig/insert',
			dataType : 'text',					
			data : {
				"sname":sname,
				"ip":ip,
				"port":port,
				"dbtype":dbtype,
				"username" : username,
				"password" : password,
				"defaultdb":defaultdb,
				"sid":sid
			},
			success : function(msg) {
				console.log(msg);
				if(msg=="1"){
					alert("录入成功");
					
					$("#servername").val('');
					$("#serverip").val('');
					$("#port").val('');
					$("#dbtype").val('');
					$("#username").val('');
					$("#password").val('');
					$("#defaultdb").val('');
					$("#sid").val('');
					
				}else{
					alert("录入失败");
				}
			},
			error : function(msg) {
				console.log('failed')
			}
		});    		
	});
	
	$("#dbtype").change(function(){
		var selected=$("#dbtype").val();
		if(selected=='Oracle'){
			$("#sid-div").show();
		}else{
			$("#sid-div").hide();
		}
	});
	
});